'use strict';

const db = uniCloud.database();
const collection = db.collection('food-banner');

exports.main = async (event, context) => {
  const { bannerId } = event.data || {};
  
  if (!bannerId) {
    return {
      code: 400,
      message: '轮播图ID不能为空',
      data: null
    };
  }
  
  try {
    // 检查轮播图是否存在
    const existingBanner = await collection.doc(bannerId).get();
    
    if (!existingBanner.data || existingBanner.data.length === 0) {
      return {
        code: 404,
        message: '轮播图不存在',
        data: null
      };
    }
    
    // 执行删除
    const result = await collection.doc(bannerId).remove();
    
    if (result.deleted === 1) {
      return {
        code: 200,
        message: '删除成功',
        data: {
          _id: bannerId
        }
      };
    } else {
      return {
        code: 500,
        message: '删除失败',
        data: null
      };
    }
  } catch (error) {
    return {
      code: 500,
      message: '删除失败',
      data: null
    };
  }
};